package es.udc.pfc.davidparedes.web.pages.user;

import java.util.Calendar;
import java.util.Date;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.PasswordField;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PageRenderLinkSource;

import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.userprofile.UserProfile;
import es.udc.pfc.davidparedes.model.userservice.UserProfileDetails;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.DuplicateInstanceException;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;

import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.MANAGER_OR_ABOVE)
public class RegisterVendor {
	
	@Property
	private UserProfile userProfile;
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private StoreService storeService;
    
    @Property
    private Long universeId;
    
    @Property
    private String loginName;

    @Property
    private String password;

    @Property
    private String retypePassword;

    @Property
    private String firstName;

    @Property
    private String lastName;

    @Property
    private String email;
    
    @Property
    private String nif;
    
    @Property
    private int baseHours;
    
    @Property
    private Date contractTerminationDate;
    
    @Component
    private Form registerVendorForm;
    
    @Component(id = "loginName")
    private TextField loginNameField;

    @Component(id = "password")
    private PasswordField passwordField;
    
    @Inject
    private Messages messages;
    
    @Inject 
    private PageRenderLinkSource pageLinkSource;
    
    
    void onActivate(Long id) {
    	try {
       		
    		storeService.findUniverse(id);
    		universeId = id;
    		
			} catch (InstanceNotFoundException e) {
		}
    }

    Object onValidateFromRegisterVendorForm() throws NumberFormatException, InstanceNotFoundException {

    	userProfile = userService.findUserProfile(userSession.getUserProfileId());
    	
        if (!registerVendorForm.isValid()) {
            return this;
        }
        
        if (!password.equals(retypePassword)) {
            registerVendorForm.recordError(passwordField, messages
                    .get("error-passwordsDontMatch"));
        } else {
        	
        	Calendar contractTermination = Calendar.getInstance();
        	contractTermination.setTime(contractTerminationDate);
        
        	try {
        		storeService.findUniverse(universeId);
                userService.registerVendor(loginName, password,
                    new UserProfileDetails(firstName, lastName, email, nif, userProfile.getStore().getStoreId()),
                    contractTermination, baseHours, universeId);
            } catch (DuplicateInstanceException e) {
                registerVendorForm.recordError(loginNameField, messages
                        .get("error-loginNameAlreadyExists"));
            } catch (InstanceNotFoundException e) {
            	registerVendorForm.recordError(loginNameField, messages
                        .get("error-storeDoesNotExist"));
            }

        }
        
        return pageLinkSource.createPageRenderLinkWithContext("user/UniverseStaff", universeId);
        
    }

}
